package tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBTools {
	private static final long serialVersionUID = 1L;
	private static final String DRIVER = "oracle.jdbc.OracleDriver";
	private static String url = "jdbc:oracle:thin:@//localhost:1521/XE";
	private static String user = "shop";
	private static String pass = "123";
	
	 private DBTools() {}
	 
	 static {
		 try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	 }

	 static DBTools dBTools = null;
	 public static DBTools getInstance() {
		 if(dBTools == null) {
			 synchronized (DBTools.class) {
				if(dBTools==null) {
					dBTools = new DBTools();
				}
			}
		 }
		 return dBTools;
	 }

	public Connection getConnection() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(url, user, pass);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	public void closeConnection(Connection conn,PreparedStatement ps,  ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				if(ps!=null) {
					try {
						ps.close();
					} catch (SQLException e) {
						e.printStackTrace();
					} finally {
						if(conn!=null) {
							try {
								conn.close();
							} catch (SQLException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		}
	}
	
	
	public static void main(String[] args) {
		DBTools db = new DBTools();
		db.getConnection();
	}
}
